package com.chy.blog_server.modular.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoMusic;
import com.chy.blog_server.modular.web.mapper.BoMusicMapper;
import com.chy.blog_server.modular.web.service.IBoMusicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：音乐表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/03/20 12:03
 */
@Service
@Transactional(readOnly = true)
public class BoMusicServiceImpl extends ServiceImpl<BoMusicMapper, BoMusic> implements IBoMusicService {

    private final static Logger logger = LoggerFactory.getLogger(BoMusicServiceImpl.class);

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 上传音乐
     * @param file
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> uploadMusic(MultipartFile file, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        //上传
        Map<String, Object> map = UploadFileUtil.fileUploadMusicToOne(file, UploadURLUtil.MUSIC);

        BoMusic boMusic = new BoMusic();

        if("200".equals(map.get("state") + "")){
            boMusic.setTitle(map.get("fileTitle") + "");
            boMusic.setArtist(map.get("fileArtist") + "");
            boMusic.setSize(Integer.valueOf(map.get("fileSize") + ""));
            boMusic.setMp3(map.get("fileName") + "");
            boMusic.setPoster("music_logo.jpg");
            boMusic.setIsShow(0);
            boMusic.preInsert(request);
            this.save(boMusic);
            res.put("state", "200");
            res.put("message", "成功上传");
        }

        return res;
    }

    /**
     * 查询已删除列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        BoMusic boMusic = this.getById(id);
        //删除音乐
        String path = UploadURLUtil.MUSIC + boMusic.getMp3(); //路径

        boolean flag = UploadFileUtil.deleteOneFile(path);

        logger.info(">>>>>>>>>>>>删除音乐成功<<<<<<<<<<<<<<<<");

        //删除图片成功则删表记录
        if(flag){
            int i = baseMapper.thoroughlyDelete(id);
            logger.info(">>>>>>>>>>>>删除音乐表记录成功<<<<<<<<<<<<<<<<");
            return i;

        }else{
            return 0;
        }
    }

}
